package com.hy.controller.setting;


import com.hy.common.base.RespResult;
import com.hy.common.consts.CommonConst;
import com.hy.common.excel.HyPropertyPlaceExport;
import com.hy.common.log.annotation.SysOpLogAnnotation;
import com.hy.common.utils.CommonUtils;
import com.hy.entity.dto.PlaceSearchDTO;
import com.hy.entity.dto.UpdateUserDTO;
import com.hy.entity.dto.UserSearchDTO;
import com.hy.entity.pojo.HyPlace;
import com.hy.entity.vo.HyUserVO;
import com.hy.service.HyPlaceService;
import com.hy.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/place")
public class PlaceController {
    @Resource
    private HyPropertyPlaceExport placeExport;

    @Autowired
    private HyPlaceService placeService;



    @GetMapping("/show")
    @SysOpLogAnnotation(menuName = "区域",menuBtn = "查看区域",reqParam = "'查询条件'")
    public RespResult showPlaceDetail(PlaceSearchDTO searchDTO){

        return  placeService.showPlaceByDto(searchDTO);




    }

    @PostMapping("/add")
    @SysOpLogAnnotation(menuName = "区域",menuBtn = "添加区域",reqParam = "'区域ID：'+#place.placeId")
    public RespResult addUser(@RequestBody HyPlace place){
        return placeService.addPlace(place);
    }



    @PostMapping("/update")
    @SysOpLogAnnotation(menuName = "区域",menuBtn = "修改区域",reqParam = "'区域ID：'+#place.placeId")
    public RespResult update(@RequestBody HyPlace place){
        if (place.getDel().equals(2)){
            return RespResult.error(500,"不存在的资源");
        }
        return placeService.updatePlace(place);
    }


    @PostMapping("/del")
    @SysOpLogAnnotation(menuName = "区域",menuBtn = "删除区域",reqParam = "'区域ID：'+#ids")
    public RespResult delete(Integer[] ids){
        List<Integer> integers=new ArrayList<>();
        Collections.addAll(integers,ids);
        return placeService.deletePlace(integers);

    }

    @PostMapping("/export")
    @SysOpLogAnnotation(menuName = "区域",menuBtn = "导出",reqParam = "'导出'")
    public void export(HttpServletResponse response, PlaceSearchDTO searchDTO){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        HyUserVO principal = (HyUserVO) authentication.getPrincipal();
        List<String> roles = principal.getRoles();
        if(CommonUtils.isMember(roles, CommonConst.SYSTEM_ROLE_NAME)){

        }else {
        searchDTO.setUnitId(principal.getUnitId());
        }
        placeExport.exportWithBigData(response,"区域列表",searchDTO);
    }
    @RequestMapping("/importExcelBigData")
    @SysOpLogAnnotation(menuName = "区域",menuBtn = "导入区域",reqParam = "'区域导入'")
    public RespResult importExcelBigData(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request){


        return  placeService.importExcelBigData(multipartFile,request);
    }




}
